
// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

clear

import dbase using "local\gis_data\016_society\refugee\ner_idpref_fishnet.dbf", clear

keep IDPREF FID_1
rename FID_1 objectid
rename IDPREF idpref

collapse (sum) idpref, by(objectid)

g source = "reliefweb.int"

save "local\gis_data\016_society\refugee\ner_idpref_fishnet.dta", replace

* tchad
import dbase using "local\gis_data\016_society\refugee\tcd_idpref_fishnet.dbf", clear
keep OBJECTID_1 IDPREF
rename OBJECTID_1 objectid
rename IDPREF idpref

collapse (sum) idpref, by(objectid)

g source = "reliefweb.int"

save "local\gis_data\016_society\refugee\tcd_idpref.dta", replace

* cmr - area
import dbase using "local\gis_data\016_society\refugee\cmr_idpref_fishnet.dbf", clear

keep FID_idp_ar OBJECTID_1 IDPREF AREA_KM2

rename OBJECTID_1 objectid
rename IDPREF idpref
rename AREA_KM2 area_km2

save "local\gis_data\016_society\refugee\cmr_idpref_fishnet.dta", replace

collapse (sum) area_km2, by(FID_idp_ar)

rename area_km2 idpref_area_km2

save "local\gis_data\016_society\refugee\cmr_idpref_area.dta", replace

clear
use "local\gis_data\016_society\refugee\cmr_idpref_fishnet.dta", replace

sort FID_idp_ar
merge m:1 FID_idp_ar using "local\gis_data\016_society\refugee\cmr_idpref_area.dta"

sort FID_idp_ar
g sh_idpref_area_km2 = area_km2 / idpref_area_km2

*replace idpref = idpref *  sh_idpref_area_km2
g idpref_den = idpref *  sh_idpref_area_km2

drop FID_idp_ar idpref area_km2 idpref_area_km2 _merge sh_idpref_area_km2

sort objectid

rename idpref_den idpref

save "local\gis_data\016_society\refugee\cmr_idpref_fishnet.dta", replace


**
**
**

append using "local\gis_data\016_society\refugee\ner_idpref_fishnet.dta"


append using  "local\gis_data\016_society\refugee\cmr_idpref_fishnet.dta"

**
**

collapse (sum) idpref, by(objectid)


label variable idpref "IDP + Refugees : reliefweb.int"


save "proc_data\IDPREF.dta", replace


**
** add refugee camp dummy
**

clear
import dbase using "local\gis_data\016_society\refugee\refugee_camps_w_fishnet.dbf"

keep OBJECTID_1 name
rename OBJECTID_1 objectid
rename name refcamp_name
g d_refcamp = 1
label variable d_refcamp "dummy refugee camps : UNHCR"
label variable refcamp_name "Refugee camp name : UNHCR"
label variable objectid "grid id : GIS"
sort objectid
merge 1:1 objectid using "proc_data\IDPREF.dta"
drop _merge

sort objectid
replace d_refcamp = 0 if d_refcamp==.

**
replace idpref = 7868 if refcamp_name == "Dar es Salam"

save "proc_data\IDPREF.dta", replace